package com.edu.bhu.admin.controller.admin;

import java.util.List;

import com.edu.bhu.admin.domain.PreArea;
import com.edu.bhu.admin.domain.PreRecord;
import com.edu.bhu.admin.service.IPreAreaService;
import com.edu.bhu.admin.service.IPreRecordService;
import com.edu.bhu.admin.util.AreaBean;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.core.page.TableDataInfo;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.common.utils.ShiroUtils;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 记录Controller
 *
 * @author yutianyang
 * @date 2022-04-06
 */
@Controller
@RequestMapping("/admin/record")
public class PreRecordController extends BaseController
{
    private String prefix = "admin/record";

    @Autowired
    private IPreRecordService preRecordService;

    @Autowired
    private IPreAreaService preAreaService;

    @RequiresPermissions("admin:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 我提交的记录
     */
    @RequiresPermissions("admin:record:view")
    @GetMapping("/myRecord")
    public String myRecord()
    {
        return prefix + "/myRecord";
    }

    /**
     * 查询记录列表
     */
    @RequiresPermissions("admin:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PreRecord preRecord)
    {
        startPage();
        List<PreRecord> list = preRecordService.selectPreRecordList(preRecord);
        return getDataTable(list);
    }

    /**
     * 查询地区列表，形式为AreaBean的树形结构
     */
    @RequiresPermissions("admin:area:list")
    @GetMapping("/treeList")
    @ResponseBody
    public List<AreaBean> treeList(PreArea preArea)
    {
        List<AreaBean> list = preAreaService.selectAreaBeanList();
        return list;
    }

    /**
     * 查询我的记录列表
     */
    @RequiresPermissions("admin:record:list")
    @PostMapping("/myList")
    @ResponseBody
    public TableDataInfo myList(PreRecord preRecord)
    {
        preRecord.setUserId(ShiroUtils.getSysUser().getUserId());
        startPage();
        List<PreRecord> list = preRecordService.selectPreRecordList(preRecord);
        return getDataTable(list);
    }

    /**
     * 导出记录列表
     */
    @RequiresPermissions("admin:record:export")
    @Log(title = "记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PreRecord preRecord)
    {
        List<PreRecord> list = preRecordService.selectPreRecordList(preRecord);
        ExcelUtil<PreRecord> util = new ExcelUtil<PreRecord>(PreRecord.class);
        return util.exportExcel(list, "记录数据");
    }

    /**
     * 新增记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存记录
     */
    @RequiresPermissions("admin:record:add")
    @Log(title = "记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PreRecord preRecord, Integer province, Integer city, Integer area)
    {
        if(area != null){
            preRecord.setAreaId(area);
        }else if(city != null){
            preRecord.setAreaId(city);
        }else if(province != null){
            preRecord.setAreaId(province);
        }
        return toAjax(preRecordService.insertPreRecord(preRecord));
    }

    /**
     * 修改记录
     */
    @RequiresPermissions("admin:record:edit")
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap)
    {
        PreRecord preRecord = preRecordService.selectPreRecordByRecordId(recordId);
        mmap.put("preRecord", preRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存记录
     */
    @RequiresPermissions("admin:record:edit")
    @Log(title = "记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PreRecord preRecord)
    {
        return toAjax(preRecordService.updatePreRecord(preRecord));
    }

    /**
     * 删除记录
     */
    @RequiresPermissions("admin:record:remove")
    @Log(title = "记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(preRecordService.deletePreRecordByRecordIds(ids));
    }
}
